package com.china08.yxyapi.security.authorizer;

import org.apache.commons.lang3.StringUtils;
import org.pac4j.core.authorization.authorizer.AbstractRequireElementAuthorizer;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpAction;

import com.china08.yxyapi.security.profile.YxyProfile;

public class CurrentRoleIdAuthorizer<U extends YxyProfile> extends AbstractRequireElementAuthorizer<String, U> {

    public CurrentRoleIdAuthorizer(String roleId) {
        setElements(roleId);
    }

    @Override
    protected boolean check(WebContext context, U profile, String element) throws HttpAction {
        String roleId = profile.getRoleId();
        if (StringUtils.isEmpty(roleId)) { return false; }
        if (roleId.equals(element)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected boolean isProfileAuthorized(WebContext context, U profile) throws HttpAction {
        if (profile != null) {
            return true;
        } else {
            return false;
        }
    }

}
